/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package chain

import (
	"testing"

	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/client/chain"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/client/iam"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/core/config"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/ichain_sdk"
	iam2 "git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/iam"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/test/data"
	"github.com/stretchr/testify/assert"
)

func TestChainInvoke(t *testing.T) {
	env := data.GetEnvFromFlag()
	assert.NotNil(t, env)
	builder := env.SdkBuilder()
	sdk, err := ichain_sdk.New(config.FromBuilder(builder))
	assert.NoError(t, err)
	defer sdk.Close()
	iamClient, err := iam.New(sdk.Context())
	assert.NoError(t, err)

	identity, err := iamClient.CreateSigningIdentity(iam2.WithCert(env.ClientCert()),
		iam2.WithPrivateKey(env.ClientPvk()))
	assert.NoError(t, err)
	chainClient, err := chain.New(sdk.ChainContext(env.ChainId(), ichain_sdk.WithIdentity(identity)))
	if !assert.NoError(t, err) {
		return
	}

	// 测试调用合约
	resp, err := chainClient.Execute(env.RandInvokeFunc())
	assert.NoError(t, err)
	assert.NotEmpty(t, resp.Responses)
}
